<!DOCTYPE html>
<html>
<head>
    

    

    
<!-- Baidu Tongji -->
<script>var _hmt = _hmt || []</script>
<script async src="//hm.baidu.com/hm.js?a7c05ce530152d9866930ef4850ee566"></script>
<!-- End Baidu Tongji -->




    <meta charset="utf-8">
    
    
    
    <title>tomcat ManagerServlet阅读源码后的实践 | 神奇的鸭鸭の码农库 | 新知识要不断的总结记录成笔记，要多写，多画，能够清晰透彻的将知识讲给别人听，才是达到理解的层次。</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    
    <meta name="theme-color" content="#765959">
    
    
    <meta name="keywords" content="源码">
    <meta name="description" content="介绍源码链接：http://svn.apache.org/repos/asf/tomcat/tc7.0.x/trunk/java/org/apache/catalina/manager/ManagerServlet.java ManagerServlet是tomcat的部署源码，通过他我们可以获取tomcat的部分指标，远程管理web应用程序，不过这个功能将受到web应用程序部署描述符中的安全约束">
<meta name="keywords" content="源码">
<meta property="og:type" content="article">
<meta property="og:title" content="tomcat ManagerServlet阅读源码后的实践">
<meta property="og:url" content="http://magic_duck.oschina.io/2017/11/27/tomcat_managerServlet/index.html">
<meta property="og:site_name" content="神奇的鸭鸭の码农库">
<meta property="og:description" content="介绍源码链接：http://svn.apache.org/repos/asf/tomcat/tc7.0.x/trunk/java/org/apache/catalina/manager/ManagerServlet.java ManagerServlet是tomcat的部署源码，通过他我们可以获取tomcat的部分指标，远程管理web应用程序，不过这个功能将受到web应用程序部署描述符中的安全约束">
<meta property="og:updated_time" content="2017-12-05T10:00:15.087Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="tomcat ManagerServlet阅读源码后的实践">
<meta name="twitter:description" content="介绍源码链接：http://svn.apache.org/repos/asf/tomcat/tc7.0.x/trunk/java/org/apache/catalina/manager/ManagerServlet.java ManagerServlet是tomcat的部署源码，通过他我们可以获取tomcat的部分指标，远程管理web应用程序，不过这个功能将受到web应用程序部署描述符中的安全约束">
    
    <link rel="shortcut icon" href="/favicon.ico">
    <link rel="stylesheet" href="/css/style.css?v=1.4.3">
    <script>window.lazyScripts=[]</script>
</head>

<body>
    <div id="loading" class="active"></div>

    <aside id="menu" class="hide" >
  <div class="inner flex-row-vertical">
    <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menu-off">
        <i class="icon icon-lg icon-close"></i>
    </a>
    <div class="brand-wrap">
      <div class="brand">
        <a href="/" class="avatar waves-effect waves-circle waves-light">
          <img src="/img/avatar.jpg">
        </a>
        <hgroup class="introduce">
          <h5 class="nickname">神奇的鸭鸭</h5>
          <a href="mailto:702038338@qq.com" title="702038338@qq.com" class="mail">702038338@qq.com</a>
        </hgroup>
      </div>
    </div>
    <div class="scroll-wrap flex-col">
      <ul class="nav">
        
            <li class="waves-block waves-effect">
              <a href="/"  >
                <i class="icon icon-lg icon-home"></i>
                主页
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/archives"  >
                <i class="icon icon-lg icon-archives"></i>
                归档
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/tags"  >
                <i class="icon icon-lg icon-tags"></i>
                标签
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://git.oschina.net/magic_duck" target="_blank" >
                <i class="icon icon-lg icon-gg-circle"></i>
                码云
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://www.zhihu.com/people/shen-qi-de-ya-ya" target="_blank" >
                <i class="icon icon-lg icon-twitter"></i>
                知乎
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/404.html"  >
                <i class="icon icon-lg icon-link"></i>
                404页面测试
              </a>
            </li>
        
      </ul>
    </div>
  </div>
</aside>

    <main id="main">
        <header class="top-header" id="header">
    <div class="flex-row">
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light on" id="menu-toggle">
          <i class="icon icon-lg icon-navicon"></i>
        </a>
        <div class="flex-col header-title ellipsis">tomcat ManagerServlet阅读源码后的实践</div>
        
        <div class="search-wrap" id="search-wrap">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="back">
                <i class="icon icon-lg icon-chevron-left"></i>
            </a>
            <input type="text" id="key" class="search-input" autocomplete="off" placeholder="输入感兴趣的关键字">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="search">
                <i class="icon icon-lg icon-search"></i>
            </a>
        </div>
        
        
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menuShare">
            <i class="icon icon-lg icon-share-alt"></i>
        </a>
        
    </div>
</header>
<header class="content-header post-header">

    <div class="container fade-scale">
        <h1 class="title">tomcat ManagerServlet阅读源码后的实践</h1>
        <h5 class="subtitle">
            
                <time datetime="2017-11-27T08:39:56.000Z" itemprop="datePublished" class="page-time">
  2017-11-27
</time>


            
        </h5>
    </div>

    

</header>


<div class="container body-wrap">
    
    <aside class="post-widget">
        <nav class="post-toc-wrap" id="post-toc">
            <h4>TOC</h4>
            <ol class="post-toc"><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#介绍"><span class="post-toc-number">1.</span> <span class="post-toc-text">介绍</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#TomcatManager"><span class="post-toc-number">2.</span> <span class="post-toc-text">TomcatManager</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#TomcatManagerResponse"><span class="post-toc-number">3.</span> <span class="post-toc-text">TomcatManagerResponse</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#测试"><span class="post-toc-number">4.</span> <span class="post-toc-text">测试</span></a></li></ol>
        </nav>
    </aside>
    
<article id="post-tomcat_managerServlet"
  class="post-article article-type-post fade" itemprop="blogPost">

    <div class="post-card">
        <h1 class="post-card-title">tomcat ManagerServlet阅读源码后的实践</h1>
        <div class="post-meta">
            <time class="post-time" title="2017年11月27日 16:39" datetime="2017-11-27T08:39:56.000Z"  itemprop="datePublished">2017-11-27</time>

            


            
<span id="busuanzi_container_page_pv" title="文章总阅读量" style='display:none'>
    <i class="icon icon-eye icon-pr"></i><span id="busuanzi_value_page_pv"></span>
</span>


            

        </div>
        <div class="post-content" id="post-content" itemprop="postContent">
            <h2 id="介绍"><a href="#介绍" class="headerlink" title="介绍"></a>介绍</h2><p>源码链接：<a href="http://svn.apache.org/repos/asf/tomcat/tc7.0.x/trunk/java/org/apache/catalina/manager/ManagerServlet.java" target="_blank" rel="external">http://svn.apache.org/repos/asf/tomcat/tc7.0.x/trunk/java/org/apache/catalina/manager/ManagerServlet.java</a></p>
<p>ManagerServlet是tomcat的部署源码，通过他我们可以获取tomcat的部分指标，远程管理web应用程序，不过这个功能将受到web应用程序部署描述符中的安全约束的保护。</p>
<p>当你请求ManagerServlet时，它会检查getPathInfo()返回的值以及相关的查询参数，以确定被请求的操作。支持以下操作和参数(从servlet路径开始):</p>
<table>
<thead>
<tr>
<th style="text-align:left">请求路径</th>
<th style="text-align:left">描述</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">/deploy?config={config-url}</td>
<td style="text-align:left">根据指定的path安装并启动一个新的web应用程序(详见源码)</td>
</tr>
<tr>
<td style="text-align:left">/deploy?config={config-url}&amp;war={war-url}/</td>
<td style="text-align:left">根据指定的path安装并启动一个新的web应用程序(详见源码)</td>
</tr>
<tr>
<td style="text-align:left">/deploy?path=/xxx&amp;war={war-url}</td>
<td style="text-align:left">根据指定的path安装并启动一个新的web应用程序(详见源码)</td>
</tr>
<tr>
<td style="text-align:left">/list</td>
<td style="text-align:left">列出所有web应用程序的上下文路径。格式为path:status:sessions(活动会话数)</td>
</tr>
<tr>
<td style="text-align:left">/reload?path=/xxx</td>
<td style="text-align:left">根据指定path重新加载web应用</td>
</tr>
<tr>
<td style="text-align:left">/resources?type=xxxx</td>
<td style="text-align:left">枚举可用的全局JNDI资源，可以限制指定的java类名</td>
</tr>
<tr>
<td style="text-align:left">/serverinfo</td>
<td style="text-align:left">显示系统信息和JVM信息</td>
</tr>
<tr>
<td style="text-align:left">/sessions</td>
<td style="text-align:left">此方法已过期</td>
</tr>
<tr>
<td style="text-align:left">/expire?path=/xxx</td>
<td style="text-align:left">列出path路径下的web应用的session空闲时间信息</td>
</tr>
<tr>
<td style="text-align:left">/expire?path=/xxx&amp;idle=mm</td>
<td style="text-align:left">Expire sessions for the context path /xxx which were idle for at least mm minutes.</td>
</tr>
<tr>
<td style="text-align:left">/sslConnectorCiphers</td>
<td style="text-align:left">显示当前connector配置的SSL/TLS密码的诊断信息</td>
</tr>
<tr>
<td style="text-align:left">/start?path=/xx</td>
<td style="text-align:left">根据指定path启动web应用程序</td>
</tr>
<tr>
<td style="text-align:left">/stop?path=/xxx</td>
<td style="text-align:left">根据指定path关闭web应用程序</td>
</tr>
<tr>
<td style="text-align:left">/threaddump</td>
<td style="text-align:left">Write a JVM thread dump</td>
</tr>
<tr>
<td style="text-align:left">/undeploy?path=/xxx</td>
<td style="text-align:left">关闭并删除指定path的Web应用程序，然后删除底层WAR文件或文档基目录。</td>
</tr>
</tbody>
</table>
<p>下面列出代码实践，需要依赖httpclient和commons的一些包：</p>
<h2 id="TomcatManager"><a href="#TomcatManager" class="headerlink" title="TomcatManager"></a>TomcatManager</h2><p>封装此类方便用户请求ManagerServlet：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div><div class="line">75</div><div class="line">76</div><div class="line">77</div><div class="line">78</div><div class="line">79</div><div class="line">80</div><div class="line">81</div><div class="line">82</div><div class="line">83</div><div class="line">84</div><div class="line">85</div><div class="line">86</div><div class="line">87</div><div class="line">88</div><div class="line">89</div><div class="line">90</div><div class="line">91</div><div class="line">92</div><div class="line">93</div><div class="line">94</div><div class="line">95</div><div class="line">96</div><div class="line">97</div><div class="line">98</div><div class="line">99</div><div class="line">100</div><div class="line">101</div><div class="line">102</div><div class="line">103</div><div class="line">104</div><div class="line">105</div><div class="line">106</div><div class="line">107</div><div class="line">108</div><div class="line">109</div><div class="line">110</div><div class="line">111</div><div class="line">112</div><div class="line">113</div><div class="line">114</div><div class="line">115</div><div class="line">116</div><div class="line">117</div><div class="line">118</div><div class="line">119</div><div class="line">120</div><div class="line">121</div><div class="line">122</div><div class="line">123</div><div class="line">124</div><div class="line">125</div><div class="line">126</div><div class="line">127</div><div class="line">128</div><div class="line">129</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">import</span> java.io.File;</div><div class="line"><span class="keyword">import</span> java.net.URL;</div><div class="line"><span class="keyword">import</span> java.net.URLEncoder;</div><div class="line"></div><div class="line"><span class="keyword">import</span> org.apache.commons.io.IOUtils;</div><div class="line"><span class="keyword">import</span> org.apache.commons.lang.StringUtils;</div><div class="line"><span class="keyword">import</span> org.apache.http.Header;</div><div class="line"><span class="keyword">import</span> org.apache.http.HttpHost;</div><div class="line"><span class="keyword">import</span> org.apache.http.HttpResponse;</div><div class="line"><span class="keyword">import</span> org.apache.http.HttpStatus;</div><div class="line"><span class="keyword">import</span> org.apache.http.auth.AuthScope;</div><div class="line"><span class="keyword">import</span> org.apache.http.auth.Credentials;</div><div class="line"><span class="keyword">import</span> org.apache.http.auth.UsernamePasswordCredentials;</div><div class="line"><span class="keyword">import</span> org.apache.http.client.AuthCache;</div><div class="line"><span class="keyword">import</span> org.apache.http.client.methods.HttpGet;</div><div class="line"><span class="keyword">import</span> org.apache.http.client.methods.HttpRequestBase;</div><div class="line"><span class="keyword">import</span> org.apache.http.client.protocol.ClientContext;</div><div class="line"><span class="keyword">import</span> org.apache.http.impl.auth.BasicScheme;</div><div class="line"><span class="keyword">import</span> org.apache.http.impl.client.BasicAuthCache;</div><div class="line"><span class="keyword">import</span> org.apache.http.impl.client.DefaultHttpClient;</div><div class="line"><span class="keyword">import</span> org.apache.http.impl.conn.PoolingClientConnectionManager;</div><div class="line"><span class="keyword">import</span> org.apache.http.protocol.BasicHttpContext;</div><div class="line"></div><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">TomcatManager</span> </span>&#123;</div><div class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> String MANAGER_CHARSET = <span class="string">"UTF-8"</span>;</div><div class="line">    <span class="keyword">private</span> String username;</div><div class="line">    <span class="keyword">private</span> URL url;</div><div class="line">    <span class="keyword">private</span> String password;</div><div class="line">    <span class="keyword">private</span> String charset;</div><div class="line">    <span class="keyword">private</span> <span class="keyword">boolean</span> verbose;</div><div class="line">    <span class="keyword">private</span> DefaultHttpClient httpClient;</div><div class="line">    <span class="keyword">private</span> BasicHttpContext localContext;</div><div class="line"></div><div class="line">    <span class="comment">/**</span></div><div class="line"><span class="comment">     * constructor</span></div><div class="line"><span class="comment">     */</span></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="title">TomcatManager</span><span class="params">(URL url, String username)</span> </span>&#123;</div><div class="line">        <span class="keyword">this</span>(url, username, <span class="string">""</span>);</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="title">TomcatManager</span><span class="params">(URL url, String username, String password)</span> </span>&#123;</div><div class="line">        <span class="keyword">this</span>(url, username, password, <span class="string">"ISO-8859-1"</span>);</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="title">TomcatManager</span><span class="params">(URL url, String username, String password, String charset)</span> </span>&#123;</div><div class="line">        <span class="keyword">this</span>(url, username, password, charset, <span class="keyword">true</span>);</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="title">TomcatManager</span><span class="params">(URL url, String username, String password, String charset, <span class="keyword">boolean</span> verbose)</span> </span>&#123;</div><div class="line">        <span class="keyword">this</span>.url = url;</div><div class="line">        <span class="keyword">this</span>.username = username;</div><div class="line">        <span class="keyword">this</span>.password = password;</div><div class="line">        <span class="keyword">this</span>.charset = charset;</div><div class="line">        <span class="keyword">this</span>.verbose = verbose;</div><div class="line"></div><div class="line">        PoolingClientConnectionManager poolingClientConnectionManager = <span class="keyword">new</span> PoolingClientConnectionManager();</div><div class="line">        poolingClientConnectionManager.setMaxTotal(<span class="number">5</span>);</div><div class="line">        <span class="keyword">this</span>.httpClient = <span class="keyword">new</span> DefaultHttpClient(poolingClientConnectionManager);</div><div class="line"></div><div class="line">        <span class="keyword">if</span> (StringUtils.isNotEmpty(username)) &#123;</div><div class="line">            Credentials creds = <span class="keyword">new</span> UsernamePasswordCredentials(username, password);</div><div class="line"></div><div class="line">            String host = url.getHost();</div><div class="line">            <span class="keyword">int</span> port = url.getPort() &gt; -<span class="number">1</span> ? url.getPort() : AuthScope.ANY_PORT;</div><div class="line">            httpClient.getCredentialsProvider().setCredentials(<span class="keyword">new</span> AuthScope(host, port), creds);</div><div class="line"></div><div class="line">            AuthCache authCache = <span class="keyword">new</span> BasicAuthCache();</div><div class="line">            BasicScheme basicAuth = <span class="keyword">new</span> BasicScheme();</div><div class="line">            HttpHost targetHost = <span class="keyword">new</span> HttpHost(url.getHost(), url.getPort(), url.getProtocol());</div><div class="line">            authCache.put(targetHost, basicAuth);</div><div class="line"></div><div class="line">            localContext = <span class="keyword">new</span> BasicHttpContext();</div><div class="line">            localContext.setAttribute(ClientContext.AUTH_CACHE, authCache);</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">public</span> TomcatManagerResponse <span class="title">deploy</span><span class="params">(String path, File war, <span class="keyword">boolean</span> update)</span> <span class="keyword">throws</span> Exception </span>&#123;</div><div class="line">        StringBuilder buffer = <span class="keyword">new</span> StringBuilder(<span class="string">"/deploy"</span>);</div><div class="line">        buffer.append(<span class="string">"?path="</span>).append(URLEncoder.encode(path, charset));</div><div class="line"></div><div class="line">        <span class="keyword">if</span> (war != <span class="keyword">null</span>) &#123;</div><div class="line">            buffer.append(<span class="string">"&amp;war="</span>).append(URLEncoder.encode(war.toString(), charset));</div><div class="line">        &#125;</div><div class="line">        <span class="keyword">if</span> (update) &#123;</div><div class="line">            buffer.append(<span class="string">"&amp;update=true"</span>);</div><div class="line">        &#125;</div><div class="line">        <span class="keyword">return</span> invoke(buffer.toString());</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">private</span> TomcatManagerResponse <span class="title">invoke</span><span class="params">(String path)</span> <span class="keyword">throws</span> Exception </span>&#123;</div><div class="line">        HttpRequestBase httpRequestBase = <span class="keyword">new</span> HttpGet(url + path);</div><div class="line">        HttpResponse response = httpClient.execute(httpRequestBase, localContext);</div><div class="line"></div><div class="line">        <span class="keyword">int</span> statusCode = response.getStatusLine().getStatusCode();</div><div class="line">        <span class="keyword">switch</span> (statusCode) &#123;</div><div class="line">            <span class="keyword">case</span> HttpStatus.SC_OK: <span class="comment">// 200</span></div><div class="line">            <span class="keyword">case</span> HttpStatus.SC_CREATED: <span class="comment">// 201</span></div><div class="line">            <span class="keyword">case</span> HttpStatus.SC_ACCEPTED: <span class="comment">// 202</span></div><div class="line">                <span class="keyword">break</span>;</div><div class="line">            <span class="keyword">case</span> HttpStatus.SC_MOVED_PERMANENTLY: <span class="comment">// 301</span></div><div class="line">            <span class="keyword">case</span> HttpStatus.SC_MOVED_TEMPORARILY: <span class="comment">// 302</span></div><div class="line">            <span class="keyword">case</span> HttpStatus.SC_SEE_OTHER: <span class="comment">// 303</span></div><div class="line">            String relocateUrl = calculateRelocatedUrl(response);</div><div class="line">            <span class="keyword">this</span>.url = <span class="keyword">new</span> URL(relocateUrl);</div><div class="line">            <span class="keyword">return</span> invoke(path);</div><div class="line">        &#125;</div><div class="line"></div><div class="line">        <span class="keyword">return</span> <span class="keyword">new</span> TomcatManagerResponse().setStatusCode(response.getStatusLine().getStatusCode())</div><div class="line">                .setReasonPhrase(response.getStatusLine().getReasonPhrase())</div><div class="line">                .setHttpResponseBody(IOUtils.toString(response.getEntity().getContent()));</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">protected</span> String <span class="title">calculateRelocatedUrl</span><span class="params">(HttpResponse response)</span> </span>&#123;</div><div class="line">        Header locationHeader = response.getFirstHeader(<span class="string">"Location"</span>);</div><div class="line">        String locationField = locationHeader.getValue();</div><div class="line">        <span class="comment">// is it a relative Location or a full ?</span></div><div class="line">        <span class="keyword">return</span> locationField.startsWith(<span class="string">"http"</span>) ? locationField : url.toString() + <span class="string">'/'</span> + locationField;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">public</span> TomcatManagerResponse <span class="title">list</span><span class="params">()</span> <span class="keyword">throws</span> Exception </span>&#123;</div><div class="line">        StringBuilder buffer = <span class="keyword">new</span> StringBuilder(<span class="string">"/list"</span>);</div><div class="line">        <span class="keyword">return</span> invoke(buffer.toString());</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">public</span> TomcatManagerResponse <span class="title">serverinfo</span><span class="params">()</span> <span class="keyword">throws</span> Exception </span>&#123;</div><div class="line">        StringBuilder buffer = <span class="keyword">new</span> StringBuilder(<span class="string">"/serverinfo"</span>);</div><div class="line">        <span class="keyword">return</span> invoke(buffer.toString());</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<h2 id="TomcatManagerResponse"><a href="#TomcatManagerResponse" class="headerlink" title="TomcatManagerResponse"></a>TomcatManagerResponse</h2><p>封装响应信息：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">TomcatManagerResponse</span> </span>&#123;</div><div class="line">    <span class="keyword">private</span> <span class="keyword">int</span> statusCode;</div><div class="line"></div><div class="line">    <span class="keyword">private</span> String reasonPhrase;</div><div class="line"></div><div class="line">    <span class="keyword">private</span> String httpResponseBody;</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="title">TomcatManagerResponse</span><span class="params">()</span> </span>&#123;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">getStatusCode</span><span class="params">()</span> </span>&#123;</div><div class="line">        <span class="keyword">return</span> statusCode;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">public</span> TomcatManagerResponse <span class="title">setStatusCode</span><span class="params">(<span class="keyword">int</span> statusCode)</span> </span>&#123;</div><div class="line">        <span class="keyword">this</span>.statusCode = statusCode;</div><div class="line">        <span class="keyword">return</span> <span class="keyword">this</span>;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">getReasonPhrase</span><span class="params">()</span> </span>&#123;</div><div class="line">        <span class="keyword">return</span> reasonPhrase;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">public</span> TomcatManagerResponse <span class="title">setReasonPhrase</span><span class="params">(String reasonPhrase)</span> </span>&#123;</div><div class="line">        <span class="keyword">this</span>.reasonPhrase = reasonPhrase;</div><div class="line">        <span class="keyword">return</span> <span class="keyword">this</span>;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">getHttpResponseBody</span><span class="params">()</span> </span>&#123;</div><div class="line">        <span class="keyword">return</span> httpResponseBody;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">public</span> TomcatManagerResponse <span class="title">setHttpResponseBody</span><span class="params">(String httpResponseBody)</span> </span>&#123;</div><div class="line">        <span class="keyword">this</span>.httpResponseBody = httpResponseBody;</div><div class="line">        <span class="keyword">return</span> <span class="keyword">this</span>;</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<h2 id="测试"><a href="#测试" class="headerlink" title="测试"></a>测试</h2><p>在测试之前请先放通用户权限：<br><figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="tag">&lt;<span class="name">role</span> <span class="attr">rolename</span>=<span class="string">"admin-gui"</span>/&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">role</span> <span class="attr">rolename</span>=<span class="string">"admin-script"</span>/&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">role</span> <span class="attr">rolename</span>=<span class="string">"manager-gui"</span>/&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">role</span> <span class="attr">rolename</span>=<span class="string">"manager-script"</span>/&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">role</span> <span class="attr">rolename</span>=<span class="string">"manager-jmx"</span>/&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">role</span> <span class="attr">rolename</span>=<span class="string">"manager-status"</span>/&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">user</span> <span class="attr">username</span>=<span class="string">"admin"</span> <span class="attr">password</span>=<span class="string">"password"</span> <span class="attr">roles</span>=<span class="string">"manager-gui,manager-script,manager-jmx,manager-status,admin-script,admin-gui"</span>/&gt;</span></div></pre></td></tr></table></figure></p>
<p>下面就可以进行测试了:<br><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">import</span> <span class="keyword">static</span> org.testng.AssertJUnit.assertEquals;</div><div class="line"></div><div class="line"><span class="keyword">import</span> java.io.File;</div><div class="line"><span class="keyword">import</span> java.net.URL;</div><div class="line"></div><div class="line"><span class="keyword">import</span> org.testng.annotations.Test;</div><div class="line"></div><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">TestTomcatManager</span> </span>&#123;</div><div class="line"></div><div class="line">    <span class="meta">@Test</span></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testDeploy</span><span class="params">()</span> <span class="keyword">throws</span> Exception </span>&#123;</div><div class="line">        TomcatManager tm = <span class="keyword">new</span> TomcatManager(<span class="keyword">new</span> URL(<span class="string">"http://localhost:8080/manager/text"</span>), <span class="string">"sqdyy"</span>, <span class="string">"123456"</span>);</div><div class="line">        File war = <span class="keyword">new</span> File(<span class="string">"E:\\tomcat\\simple-war-project-1.0-SNAPSHOT.war"</span>);</div><div class="line">        TomcatManagerResponse response = tm.deploy(<span class="string">"/simple-war-project-1.0-SNAPSHOT"</span>, war, <span class="keyword">true</span>);</div><div class="line">        System.out.println(response.getHttpResponseBody());</div><div class="line">        assertEquals(<span class="number">200</span>, response.getStatusCode());</div><div class="line">        <span class="comment">// output:</span></div><div class="line">        <span class="comment">// OK - Deployed application at context path /simple-war-project-1.0-SNAPSHOT</span></div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="meta">@Test</span></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testList</span><span class="params">()</span> <span class="keyword">throws</span> Exception </span>&#123;</div><div class="line">        TomcatManager tm = <span class="keyword">new</span> TomcatManager(<span class="keyword">new</span> URL(<span class="string">"http://localhost:8080/manager/text"</span>), <span class="string">"sqdyy"</span>, <span class="string">"123456"</span>);</div><div class="line">        TomcatManagerResponse response = tm.list();</div><div class="line">        System.out.println(response.getHttpResponseBody());</div><div class="line">        assertEquals(<span class="number">200</span>, response.getStatusCode());</div><div class="line">        <span class="comment">// output:</span></div><div class="line">        <span class="comment">// OK - Listed applications for virtual host localhost</span></div><div class="line">        <span class="comment">// /:running:0:ROOT</span></div><div class="line">        <span class="comment">// /simple-war-project-1.0-SNAPSHOT:running:0:simple-war-project-1.0-SNAPSHOT</span></div><div class="line">        <span class="comment">// /examples:running:0:examples</span></div><div class="line">        <span class="comment">// /host-manager:running:0:host-manager</span></div><div class="line">        <span class="comment">// /manager:running:0:manager</span></div><div class="line">        <span class="comment">// /docs:running:0:docs</span></div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="meta">@Test</span></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testServerinfo</span><span class="params">()</span> <span class="keyword">throws</span> Exception </span>&#123;</div><div class="line">        TomcatManager tm = <span class="keyword">new</span> TomcatManager(<span class="keyword">new</span> URL(<span class="string">"http://localhost:8080/manager/text"</span>), <span class="string">"sqdyy"</span>, <span class="string">"123456"</span>);</div><div class="line">        TomcatManagerResponse response = tm.serverinfo();</div><div class="line">        System.out.println(response.getHttpResponseBody());</div><div class="line">        assertEquals(<span class="number">200</span>, response.getStatusCode());</div><div class="line">        <span class="comment">// output:</span></div><div class="line">        <span class="comment">// OK - Server info</span></div><div class="line">        <span class="comment">// Tomcat Version: Apache Tomcat/7.0.82</span></div><div class="line">        <span class="comment">// OS Name: Windows 10</span></div><div class="line">        <span class="comment">// OS Version: 10.0</span></div><div class="line">        <span class="comment">// OS Architecture: amd64</span></div><div class="line">        <span class="comment">// JVM Version: 1.8.0_144-b01</span></div><div class="line">        <span class="comment">// JVM Vendor: Oracle Corporation</span></div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>

        </div>

        <blockquote class="post-copyright">
    <div class="content">
        
<span class="post-time">
    最后更新时间：<time datetime="2017-12-05T10:00:15.087Z" itemprop="dateUpdated">2017年12月5日 18:00</time>
</span><br>


        如要转载请注明出处：<a href="/2017/11/27/tomcat_managerServlet/" target="_blank" rel="external">http://magic_duck.oschina.io/2017/11/27/tomcat_managerServlet/</a>
    </div>
    <footer>
        <a href="http://magic_duck.oschina.io">
            <img src="/img/avatar.jpg" alt="神奇的鸭鸭">
            神奇的鸭鸭
        </a>
    </footer>
</blockquote>

        
<div class="page-reward">
    <a id="rewardBtn" href="javascript:;" class="page-reward-btn waves-effect waves-circle waves-light">赏</a>
</div>



        <div class="post-footer">
            
	<ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/源码/">源码</a></li></ul>


            
<div class="page-share-wrap">
    

<div class="page-share" id="pageShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=http://magic_duck.oschina.io/2017/11/27/tomcat_managerServlet/&title=《tomcat ManagerServlet阅读源码后的实践》 — 神奇的鸭鸭の码农库&pic=http://magic_duck.oschina.io/img/avatar.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=http://magic_duck.oschina.io/2017/11/27/tomcat_managerServlet/&title=《tomcat ManagerServlet阅读源码后的实践》 — 神奇的鸭鸭の码农库&source=" data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=http://magic_duck.oschina.io/2017/11/27/tomcat_managerServlet/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《tomcat ManagerServlet阅读源码后的实践》 — 神奇的鸭鸭の码农库&url=http://magic_duck.oschina.io/2017/11/27/tomcat_managerServlet/&via=http://magic_duck.oschina.io" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=http://magic_duck.oschina.io/2017/11/27/tomcat_managerServlet/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>



    <a href="javascript:;" id="shareFab" class="page-share-fab waves-effect waves-circle">
        <i class="icon icon-share-alt icon-lg"></i>
    </a>
</div>



        </div>
    </div>

    
<nav class="post-nav flex-row flex-justify-between">
  
    <div class="waves-block waves-effect prev">
      <a href="/2017/12/05/shiro01/" id="post-prev" class="post-nav-link">
        <div class="tips"><i class="icon icon-angle-left icon-lg icon-pr"></i> Prev</div>
        <h4 class="title">shiro(一) 介绍与入门</h4>
      </a>
    </div>
  

  
    <div class="waves-block waves-effect next">
      <a href="/2017/11/20/grafana_http_api_1/" id="post-next" class="post-nav-link">
        <div class="tips">Next <i class="icon icon-angle-right icon-lg icon-pl"></i></div>
        <h4 class="title">Grafana-HTTPAPI(1)(Authentication篇+DataSource篇)</h4>
      </a>
    </div>
  
</nav>



    







</article>

<div id="reward" class="page-modal reward-lay">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <h3 class="reward-title">
        <i class="icon icon-quote-left"></i>
        我只要一角钱~ ~
        <i class="icon icon-quote-right"></i>
    </h3>
    <ul class="reward-items">
        
        <li>
            <img src="/img/wechat.png" title="微信打赏二维码" alt="微信打赏二维码">
            <p>微信</p>
        </li>
        

        
        <li>
            <img src="/img/alipay.jpg" title="支付宝打赏二维码" alt="支付宝打赏二维码">
            <p>支付宝</p>
        </li>
        
    </ul>
</div>



</div>

        <footer class="footer">
    <div class="top">
        
<p>
    <span id="busuanzi_container_site_uv" style='display:none'>
        站点总访客数：<span id="busuanzi_value_site_uv"></span>
    </span>
    <span id="busuanzi_container_site_pv" style='display:none'>
        站点总访问量：<span id="busuanzi_value_site_pv"></span>
    </span>
</p>


        <p>
            <span><a href="" target="_blank" class="rss" title="rss"><i class="icon icon-lg icon-rss"></i></a></span>
            <span>博客内容遵循 <a href="http://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">知识共享 署名 - 非商业性 - 相同方式共享 4.0协议</a></span>
        </p>
    </div>
    <div class="bottom">
        <p>
            <span>Power by <a href="http://hexo.io/" target="_blank">Hexo</a> Theme <a href="https://git.oschina.net/z77z" target="_blank">邹海清</a></span>
            <span>神奇的鸭鸭の码农库 &copy; 2017</span>
        </p>
    </div>
</footer>

    </main>
    <div class="mask" id="mask"></div>
<a href="javascript:;" id="gotop" class="waves-effect waves-circle waves-light"><span class="icon icon-lg icon-chevron-up"></span></a>



<div class="global-share" id="globalShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=http://magic_duck.oschina.io/2017/11/27/tomcat_managerServlet/&title=《tomcat ManagerServlet阅读源码后的实践》 — 神奇的鸭鸭の码农库&pic=http://magic_duck.oschina.io/img/avatar.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=http://magic_duck.oschina.io/2017/11/27/tomcat_managerServlet/&title=《tomcat ManagerServlet阅读源码后的实践》 — 神奇的鸭鸭の码农库&source=" data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=http://magic_duck.oschina.io/2017/11/27/tomcat_managerServlet/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《tomcat ManagerServlet阅读源码后的实践》 — 神奇的鸭鸭の码农库&url=http://magic_duck.oschina.io/2017/11/27/tomcat_managerServlet/&via=http://magic_duck.oschina.io" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=http://magic_duck.oschina.io/2017/11/27/tomcat_managerServlet/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>


<div class="page-modal wx-share" id="wxShare">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <p>扫一扫，分享到微信</p>
    <img src="//api.qrserver.com/v1/create-qr-code/?data=http://magic_duck.oschina.io/2017/11/27/tomcat_managerServlet/" alt="微信分享二维码">
</div>




    <script src="//cdn.bootcss.com/node-waves/0.7.4/waves.min.js"></script>
<script>
var BLOG = { ROOT: '/', SHARE: true, REWARD: true };



lazyScripts.push('//s95.cnzz.com/z_stat.php?id=1261081671&web_id=1261081671')

</script>

<script src="/js/main.min.js?v=1.4.3"></script>


<div class="search-panel" id="search-panel">
    <ul class="search-result" id="search-result"></ul>
</div>
<template id="search-tpl">
<li class="item">
    <a href="{path}" class="waves-block waves-effect">
        <div class="title ellipsis" title="{title}">{title}</div>
        <div class="flex-row flex-middle">
            <div class="tags ellipsis">
                {tags}
            </div>
            <time class="flex-col time">{date}</time>
        </div>
    </a>
</li>
</template>

<script src="/js/search.min.js?v=1.4.3" async></script>






<script async src="//dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>


</body>
</html>
